/*
 * @Author: June
 * @Date: 2022-11-14 14:40:09
 * @LastEditors: 无处安放的六月
 * @LastEditTime: 2023-12-06 14:10:21
 * @Description: axios config
 *
 */
import store from "@/store";
import { getToken, removeToken } from "@/utils/auth";
import axios from "axios";
import Vue from "vue";
// create an axios instance
const service = axios.create({
  // baseURL: "http://192.168.0.105:8001",
  // baseURL: 'http://172.16.2.22:8001',
  // baseURL: 'http://172.16.3.119:8001',
  // baseURL: "http://119.45.235.33:8080/api",

  // baseURL: 'http://36.133.166.226:48002',
  baseURL: "http://zjy.joysoft.cc:8999/api",
  // baseURL: 'http://172.16.1.180:8001', // http://192.168.1.175:8001/doc.html#/
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000,
});

// 请求服务拦截器
service.interceptors.request.use(
  (config) => {
    config.headers["Authorization"] =
      "Basic " + new Buffer("mes" + ":" + "mes").toString("base64");
    if (store.getters.token) {
      // 请求头添加token
      config.headers["Dobo-Auth"] = "bearer " + getToken();
    }

    return config;
  },
  (error) => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// 请求服务返回值 拦截器
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response;
    if (!res.data.code || res.data.code === 200) {
      return res;
    } else {
      if (
        res.data.code === 401 ||
        res.data.code === 403 ||
        res.data.code === 1002000006
      ) {
        Vue.prototype.$notify.error(res.data.msg);
        removeToken();
        location.href = `/login?redirect=${location.pathname}`;
      }

      Vue.prototype.$notify.error(res.data.msg || "请求错误,请联系管理员！");
    }
  },
  (error) => {
    if (error.response) {
      if (error.response.status === 403 || error.response.status === 401) {
        Vue.prototype.$alert(
          "用户信息过期，请重新登录！",
          store.getters.fields.TIPS,
          {
            confirmButtonText: store.getters.fields.CONFIRM,
            type: "error",
            showClose: false,
            callback: () => {
              removeToken();
              location.href = `/login?redirect=${location.pathname}`;
            },
          }
        );
      } else {
        let msg = "";
        switch (error.response.status || error.response.data.code) {
          case 404:
            msg = "请求资源未找到,请重试或联系管理员!";
            break;
          case 503:
            msg = "请求资源未找到,请重试或联系管理员!";
            break;
          case 500:
            msg = "请求错误,请联系管理员!";
            break;
          case 502:
            msg = "服务未启用,请重试或联系管理员!";
            break;
          default:
            msg = "请求错误,请联系管理员!";
            break;
        }
        Vue.prototype.$alert(msg, "提示", {
          confirmButtonText: "确认",
          type: "error",
          showClose: false,
          callback: () => {},
        });
      }
    } else {
      Vue.prototype.$notify.error("请求错误，请联系管理员！");
    }
    return Promise.reject(error);
  }
);

export default service;
